/**
 * @author gyy
 * 用户控制器
 */
package com.keepmoving.web;
import com.keepmoving.po.User;
import com.keepmoving.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author gyy
 */
@Controller
public class AdminUserController {

    @Autowired
    UserService userservice;

    //跳转导航主界面
    @RequestMapping("/underindex")
    public String index(){
        return "adminNav";
    }


    //    跳转用户列表界面
    @RequestMapping("/getUser")
    public String findUserAll(Model model){
        List<User> userList = userservice.findAll();
        model.addAttribute("userList",userList);
        return "adminUserList";
    }
    //添加
    //跳转添加信息界面
    @RequestMapping("/toadminUserAdd")
    public String toadminUserAdd(){
        return "adminUserAdd";
    }
    @RequestMapping("/adminUserAdd")
    public String adminUserAdd(@ModelAttribute User user, HttpSession session){
        User user1=(User)session.getAttribute("id");
        user.setId(user.getlId());
        userservice.addUser(user);
        return "redirect:/getUser.action";
    }
    //删除
    @RequestMapping("/adminDelect")
    public String adminDelect(int[] ids){
        System.out.println(ids);
        userservice.deleteAll(ids);

        return "redirect:/getUser.action";
    }
    @RequestMapping("/deleteUser")
    @ResponseBody
    public List<User> deleteUser(int id){
        System.out.println(id+"-------------------");
        userservice.deleteUser(id);
        List<User> userList = userservice.findAll();
        return  userList;

    }
}
